﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Pdf
{
    public class OverViewModule : BaseModule
    {
        public OverViewModule(IMetaProvider overviewProvider, Document pdfDocument, PdfFonts fontsProvider, BookConfiguration cfg)
            : base(overviewProvider, pdfDocument, fontsProvider, cfg)
        {
            HierarchyNode rootNode = metaProvider.GetRootNode();

            // Title
            tables.Add(new PdfBookPTable(TableTitle(rootNode.Name), true));
            TableParagraph();

            // Overview
            HierarchyNode nodeOverview = rootNode.Nodes[1];
            foreach (HierarchyNode node in nodeOverview.Nodes)
            {
                    tables.Add(new PdfBookPTable(TableOverview(node)));
                    TableParagraph();
            }
        }


        private PdfPTable TableOverview(HierarchyNode node)
        {
            PdfPTable table = CreatePropertyTable();

            // level one
            Phrase ph = new Phrase(node.Descr, BookFonts.FontLevelOne);
            PdfPCell cell = new PdfPCell(ph);
            cell.Border = Rectangle.NO_BORDER;
            cell.BackgroundColor = new BaseColor(BookFonts.COLOR_TABLE_HEAD);
            cell.Colspan = 2;
            cell.PaddingBottom = 3f;
            table.AddCell(cell);


            foreach (HierarchyNode nodeChild in node.Nodes)
            {
                // Title level two (if any)
                string subTitle = nodeChild.Descr.Trim();
                if (nodeChild.HasDetail)
                {
                    if (subTitle.Length > 0)
                        PropertySubTitle(table, subTitle);

                    foreach (HierarchyNode nodeInfo in nodeChild.Nodes)
                        AddInnerOverview(table, nodeInfo);
                }
                // Only text at current level ?
                else if (subTitle.Length > 0)
                {
                    ph = new Phrase(subTitle, BookFonts.FontProperty);
                    cell = new PdfPCell(ph);
                    cell.Border = Rectangle.NO_BORDER;
                    cell.SetLeading(0.0f, 1.4f);
                    cell.PaddingTop = 4f;
                    table.AddCell(cell);

                    cell = new PdfPCell(new Phrase(nodeChild.Name, BookFonts.FontStandard));
                    cell.Border = Rectangle.NO_BORDER;
                    cell.SetLeading(0.0f, 1.4f);
                    cell.PaddingTop = 4f;
                    cell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
                    table.AddCell(cell);
                }
            }
            // Add an empty cell for distance
            cell = new PdfPCell(new Phrase(string.Empty));
            cell.Border = Rectangle.NO_BORDER;
            cell.Colspan = 2;
            cell.PaddingBottom = 3f;
            table.AddCell(cell);

            return table;
        }

        private void AddInnerOverview(PdfPTable table, HierarchyNode node)
        {
            // if has value in current language
            if (node.Name.Length > 0)
            {
                // get refered concept name in selected language
                Phrase ph = new Phrase(node.Descr, node.Id.StartsWith("_") ? 
                    BookFonts.FontPropertyItalic : BookFonts.FontProperty);
                PdfPCell cell = new PdfPCell(ph);
                cell.Border = Rectangle.NO_BORDER;
                cell.SetLeading(0.0f, 1.4f);

                cell.PaddingTop = 4f;
                table.AddCell(cell);

                ph = new Phrase(node.Name, BookFonts.FontStandard);
                cell = new PdfPCell(ph);
                cell.Border = Rectangle.NO_BORDER;
                cell.PaddingTop = 4f;
                cell.SetLeading(0.0f, 1.4f);
                cell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
                table.AddCell(cell);

                foreach (HierarchyNode nodeInfo in node.Nodes)
                    AddInnerOverview(table, nodeInfo);

            }
        }

    }
}
